package com.xjoyt.pay.operater.auth.po;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xjoyt.pay.constants.DateConstant;

@Entity
@Table(name = "account_user")
public class AccountUser {

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name = "id", length = 20)
	public long id;

	/**
	 * 所属公司
	 */
	@Column(name = "company_id", length = 36)
	public String companyId;

	/**
	 * 账号
	 */
	@Column(name = "account", length = 50)
	public String account;

	/**
	 * 手机号
	 */
	@Column(name = "phone", length = 50)
	public String phone;

	/**
	 * 姓名
	 */
	@Column(name = "user_name", length = 50)
	public String userName;

	/**
	 * 加密后的密码
	 */
	@Column(name = "password", length = 100)
	@JsonIgnore
	public String password;

	/**
	 * 加密后加码
	 */
	@Column(name = "salt", length = 4)
	@JsonIgnore
	public String salt;

	/**
	 * 是否为管理员
	 */
	@org.hibernate.annotations.Type(type = "yes_no")
	@Column(name = "is_admin")
	public Boolean admin;

	/**
	 * Email
	 */
	@Column(name = "email", length = 50)
	public String email;

	/**
	 * 所属部门
	 */
	@ManyToOne(targetEntity = Department.class, cascade = {})
	@JoinColumn(name = "department_id", foreignKey = @ForeignKey(name = "none"))
	public Department department;

	/**
	 * 最后一次登录时间
	 */
	@Column(name = "last_login_time")
	@JsonFormat(pattern = DateConstant.DATE_TIME_FORMAT_PATTERN, timezone = "GMT+8")
	public Date lastLoginTime;

	/**
	 * 状态：在职、离职
	 */
	@Column(name = "is_enabled")
	@org.hibernate.annotations.Type(type = "yes_no")
	public Boolean enabled;

	/**
	 * 角色列表
	 */
	@OneToMany(targetEntity = UserRole.class, cascade = { CascadeType.REMOVE })
	@JoinColumn(name = "user_id", foreignKey = @ForeignKey(name = "none"))
	public List<UserRole> userRoleList;
	
	public String getRoles(){
		String roles="";
		if(userRoleList!=null){
			int i = 0 ;
			for(UserRole userRole:userRoleList){
				roles = roles+userRole.role.name+",";
			}
			roles = roles.substring(0, roles.length()-1);
		}
		return roles;
	}

	/**
	 * 录入日期
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "added_date")
	@JsonFormat(pattern = DateConstant.DATE_TIME_FORMAT_PATTERN, timezone = "GMT+8")
	public Date addedDate;

	/**
	 * 角色描述
	 */
	@Column(name = "remark", length = 255)
	public String remark;
	
	/**
	 * 角色类别
	 */
	@Column(name = "user_type", length = 2)
	public Integer userType;
}
